package com.vivz.baby;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

import com.vivz.baby.domain.ParcelableLogsList;
import com.vivz.baby.persistence.model.Log;
import com.vivz.baby.service.LogService;
import com.vivz.baby.service.LogServiceReceiver;
import com.vivz.baby.service.LogServiceReceiver.Listener;

public class ListingActivity extends Activity implements Listener {

	private static final String TAG = "ListingActivity";
	private static final SimpleDateFormat SDF = new SimpleDateFormat("MM-dd-yyyy");

	private static Date from;
	private static Date to;
	private static final List<Log> logs = new ArrayList<Log>();

	private EditText editTextFrom;
	private EditText editTextTo;

	private TableLayout listingTableLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listing);
		initializeTimeFrame();
		initializeUI();
		refreshUI();

		startService(createCallingIntent());
	}

	private Intent createCallingIntent() {
		Intent i = new Intent(this, LogService.class);
		LogServiceReceiver receiver = new LogServiceReceiver(new Handler());
		receiver.setListener(this);
		i.putExtra(LogService.LOG_RECEIVER, receiver);
		String logUrl = getString(R.string.url_aws_domain) + getString(R.string.partial_url_get_all);
		i.putExtra(LogService.LOG_URL, logUrl);
		return i;
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (to == null || from == null) {
			initializeTimeFrame();
		}
		refreshUI();
	}

	private void initializeUI() {

		editTextFrom = (EditText) findViewById(R.id.edit_text_from_id);
		editTextTo = (EditText) findViewById(R.id.edit_text_to_id);

		int year = DateUtils.toCalendar(from).get(Calendar.YEAR);
		int monthOfYear = DateUtils.toCalendar(from).get(Calendar.MONTH);
		int dayOfMonth = DateUtils.toCalendar(from).get(Calendar.DAY_OF_MONTH);
		editTextFrom.setText((monthOfYear + 1) + "-" + dayOfMonth + "-" + year);

		year = DateUtils.toCalendar(to).get(Calendar.YEAR);
		monthOfYear = DateUtils.toCalendar(to).get(Calendar.MONTH);
		dayOfMonth = DateUtils.toCalendar(to).get(Calendar.DAY_OF_MONTH);
		editTextTo.setText((monthOfYear + 1) + "-" + dayOfMonth + "-" + year);

		// init table
		listingTableLayout = (TableLayout) findViewById(R.id.listing_tbl_layout_id);
		listingTableLayout.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		listingTableLayout.setScrollbarFadingEnabled(true);
		listingTableLayout.removeAllViews();
	}

	private void refreshUI() {
		if (listingTableLayout == null) {
			listingTableLayout = (TableLayout) findViewById(R.id.listing_tbl_layout_id);
		}
		listingTableLayout.removeAllViews();

		int i = 0;

		for (Log log : logs) {
			TextView child = new TextView(this);

			// child.setText(log.getId() + " || " + log.getTimestamp() + " || "
			// + log.getUpdateUser());
			child.setText(log.toString());

			listingTableLayout.addView(child);

			// TODO: Remove me after handling pagination
			if (i++ > 10) {
				break;
			}
		}
	}

	private void initializeTimeFrame() {
		final Calendar c = Calendar.getInstance();
		to = c.getTime();
		c.add(Calendar.YEAR, -20);
		from = c.getTime();
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		ParcelableLogsList allLogs = resultData.getParcelable(LogService.ALL_LOGS);
		logs.clear();
		if (allLogs != null) {
			logs.addAll(allLogs.getLogs());
		}
		refreshUI();
	}

}